package cn.fuxi2022.module.passage.controller;

import cn.fuxi2022.core.exception.SmartGroupException;
import cn.fuxi2022.core.utils.JwtUtils;
import cn.fuxi2022.model.empty.applets.GroupPassage;
import cn.fuxi2022.model.empty.applets.GroupStudentInfo;
import cn.fuxi2022.model.resp.R;
import cn.fuxi2022.module.info.service.GroupStudentInfoService;
import cn.fuxi2022.module.passage.service.GroupPassageService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * 获取公众号文章列表，小程序使用
 */
@RestController
@RequestMapping("/user/passage")
public class PassageController {

    @Autowired
    private GroupPassageService groupPassageService;

    @Autowired
    private GroupStudentInfoService groupStudentInfoService;

    private GroupStudentInfo check(HttpServletRequest request) {
        // 判断用户阶段
        String id = JwtUtils.getMemberIdByJwtToken(request);
        LambdaQueryWrapper<GroupStudentInfo> infoWrapper = new LambdaQueryWrapper<>();
        infoWrapper.eq(GroupStudentInfo::getStudentId, id);
        GroupStudentInfo studentInfo = groupStudentInfoService.getOne(infoWrapper);
        // 不是阶段2（加群阶段）或阶段3（完成阶段），就是非法的。
        if (studentInfo.getIsStudent() != 2 && studentInfo.getIsStudent() != 3) {
            throw new SmartGroupException("非法访问，请检查是否完成信息录入或支付");
        }
        return studentInfo;
    }

    /**
     * 获取学习相关文章列表
     */
    @GetMapping("/getStudyList")
    public R getStudyList() {
        LambdaQueryWrapper<GroupPassage> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(GroupPassage::getType, 2).eq(GroupPassage::getStatus, 0);
        return R.ok().data(groupPassageService.list(wrapper));
    }

    /**
     * 获取入社业务所需的文章列表
     */
    @GetMapping("/getJoinList")
    public R getJoinList(HttpServletRequest request) {
        check(request);
        LambdaQueryWrapper<GroupPassage> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(GroupPassage::getType, 1).eq(GroupPassage::getStatus, 0);
        return R.ok().data(groupPassageService.list(wrapper));
    }
}
